package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.entity.User;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private IUserService userService;

    // 完成关注与取消关注的功能,followuserid是要关注(取消关注)的用户id
    // isfollow是代表当前操作是点赞还是取消点赞的flag
    @Override
    public Result follow(Long followuserid, Boolean isfollow) {
        Long userid = UserHolder.getUser().getId();
        if(isfollow){
            // 关注功能,向mysql中插入新的关注联系
            Follow follow = new Follow();
            follow.setUserId(userid);
            follow.setFollowUserId(followuserid);
            boolean success = save(follow);

            // 将新关注的博主存入到redis中,用于记录用户都关注了哪些人,为后续展示不同用户之间的共同关注做基础
            if(success) {
                stringRedisTemplate.opsForSet().add("follows:" + userid, followuserid.toString());
            }
        }else {
            // 删除关注联系
            boolean success = remove(new QueryWrapper<Follow>().eq("user_id", userid).eq("follow_user_id", followuserid));
            // 同样,删除关注的时候同时要将关注用户从redis删除
            if(success) {
                stringRedisTemplate.opsForSet().remove("follows:" + userid, followuserid.toString());
            }
        }
        return Result.ok();
    }

    // 判断当前登录用户是否关注过传入用户
    @Override
    public Result isFollow(Long id) {
        Long userid = UserHolder.getUser().getId();

        Integer count = query().eq("user_id", userid).eq("follow_user_id", id).count();

        return Result.ok(count>0);
    }

    // 返回当前用户与目标用户之间的共同关注
    @Override
    public Result commonfollow(Long id) {
        Long userid = UserHolder.getUser().getId();

        // key1是当前登录用户的redis的key
        String key1 = "follows:" + userid;
        // key2是当前查看用户的redis的key
        String key2 = "follows:" + id;

        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key1, key2);
        if(intersect == null || intersect.isEmpty()) {
            return Result.ok(Collections.emptyList());
        }

        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());

        List<UserDTO> userDTOS = userService.listByIds(ids).stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());

        return Result.ok(userDTOS);
    }
}
